﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eirene.Core.Model;
using Spring.Context;
using System.Diagnostics.Contracts;

namespace Eirene.WPF.ViewModel
{
    public class MainWindowViewModel
    {
        #region Util

        /// <summary>
        /// Loads the default main window view model.
        /// The default is loaded from IoC.
        /// </summary>
        public static MainWindowViewModel LoadDefault()
        {
            Contract.Ensures(Contract.Result<MainWindowViewModel>() != null);

            // Load the items from IoC.
            IApplicationContext ioc;
            ioc = Spring.Context.Support.ContextRegistry.GetContext();
            var viewModel = ioc.GetObject("MainWindowViewModel") as MainWindowViewModel;

            if (viewModel == null)
            {
                var msg = String.Format(
                    "Expected a {0} with id {1} be defined in IoC.",
                    typeof(MainWindowViewModel).FullName,
                    "MainWindowViewModel");

                throw new ApplicationException(msg);
            }

            return viewModel;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or set a welcome message for the application.
        /// </summary>
        public string Welcome { get; set; }

        /// <summary>
        /// Gets or sets a list of hat items. 
        /// </summary>
        public ItemContainer Hats { get; set; }

        /// <summary>
        /// Retrieve the list of owned hats as a set of view-model objects.
        /// </summary>
        public IEnumerable<ItemViewModel> Items
        {
            get
            {
                foreach (var next in this.Hats)
                {
                    yield return new ItemViewModel(next);
                }
            }
        }

        #endregion
    }
}
